package com.tc.controller;

import com.tc.dto.Patient_drug_orderDTO;
import com.tc.dto.Patient_drug_orderQueryDTO;
import com.tc.service.IPatient_drug_orderService;
import com.tc.utils.ResultVo;
import com.tc.vo.PageResultVO;
import com.tc.vo.Patient_drug_orderVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  患者药品医嘱前端控制器
 * </p>
 *
 * @author tc
 * @since 2025-07-09
 */
@RestController
@RequestMapping("/patient_drug_order")
@Api(tags = "患者药品医嘱管理")
public class Patient_drug_orderController {

    @Autowired
    private IPatient_drug_orderService patientDrugOrderService;

    @ApiOperation(value = "分页查询患者药品医嘱")
    @PostMapping("/page")
    public ResultVo<PageResultVO<Patient_drug_orderVO>> pageQuery(@RequestBody Patient_drug_orderQueryDTO queryDTO) {
        PageResultVO<Patient_drug_orderVO> pageResultVO = patientDrugOrderService.pageQuery(queryDTO);
        return ResultVo.ok(pageResultVO);
    }
    
    @ApiOperation(value = "根据患者ID查询药品医嘱列表")
    @ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/patient/{patientId}")
    public ResultVo<List<Patient_drug_orderVO>> getByPatientId(@PathVariable Long patientId) {
        List<Patient_drug_orderVO> list = patientDrugOrderService.getByPatientId(patientId);
        return ResultVo.ok(list);
    }

    @ApiOperation(value = "根据ID查询患者药品医嘱")
    @ApiImplicitParam(name = "id", value = "医嘱ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/{id}")
    public ResultVo<Patient_drug_orderVO> getById(@PathVariable Long id) {
        Patient_drug_orderVO vo = patientDrugOrderService.getPatientDrugOrderById(id);
        if (vo != null) {
            return ResultVo.ok(vo);
        } else {
            return ResultVo.fail("医嘱不存在");
        }
    }

    @ApiOperation(value = "新增患者药品医嘱")
    @PostMapping("/add")
    public ResultVo<Boolean> add(@RequestBody @Valid Patient_drug_orderDTO patientDrugOrderDTO) {
        boolean success = patientDrugOrderService.addPatientDrugOrder(patientDrugOrderDTO);
        if (success) {
            return ResultVo.ok(true, "新增成功");
        } else {
            return ResultVo.fail("新增失败");
        }
    }

    @ApiOperation(value = "修改患者药品医嘱")
    @PutMapping("/update")
    public ResultVo<Boolean> update(@RequestBody @Valid Patient_drug_orderDTO patientDrugOrderDTO) {
        if (patientDrugOrderDTO.getOrderId() == null) {
            return ResultVo.fail("医嘱ID不能为空");
        }
        
        boolean success = patientDrugOrderService.updatePatientDrugOrder(patientDrugOrderDTO);
        if (success) {
            return ResultVo.ok(true, "修改成功");
        } else {
            return ResultVo.fail("修改失败");
        }
    }

    @ApiOperation(value = "删除患者药品医嘱")
    @ApiImplicitParam(name = "id", value = "医嘱ID", required = true, dataType = "Long", paramType = "path")
    @DeleteMapping("/{id}")
    public ResultVo<Boolean> delete(@PathVariable Long id) {
        boolean success = patientDrugOrderService.deletePatientDrugOrder(id);
        if (success) {
            return ResultVo.ok(true, "删除成功");
        } else {
            return ResultVo.fail("删除失败");
        }
    }
    
    @ApiOperation(value = "批量删除患者药品医嘱")
    @DeleteMapping("/batch")
    public ResultVo<Boolean> batchDelete(@RequestBody List<Long> ids) {
        boolean success = patientDrugOrderService.removeByIds(ids);
        if (success) {
            return ResultVo.ok(true, "批量删除成功");
        } else {
            return ResultVo.fail("批量删除失败");
        }
    }
    
    @ApiOperation(value = "测试JSON解析")
    @PostMapping("/test-json")
    public ResultVo<Map<String, Object>> testJsonParsing(@RequestBody Map<String, Object> requestData) {
        // 简单返回收到的数据，用于测试JSON解析
        return ResultVo.ok(requestData, "JSON解析成功");
    }
}

